const express = require("express");
const Result = require("../../models/Result");
const { decoded } = require("../../utils/index");
const { PRIVATE_KEY, JWT_EXPIRED } = require("../../utils/constant");
const {
  getSemesterList,
  editSemester,
  createSemester,
  deleteSemester,
} = require("../../services/semester");
const jwt = require("jsonwebtoken");
const jwtAuth = require("../jwt");
// 注册路由
const router = express.Router();
router.use(jwtAuth);

//获取学期列表接口
router.get("/list", function (req, res, next) {
  getSemesterList(req.query)
    .then((data) => {
      new Result(data, "学期信息查询成功").success(res);
    })
    .catch((err) => {
      new Result("学期信息查询失败").fail(res);
    });
});

//编辑学期接口
router.post("/edit", function (req, res, next) {
  editSemester(req.body)
    .then((data) => {
      new Result("操作成功").success(res);
    })
    .catch((err) => {
      new Result("操作失败").fail(res);
    });
});

//创建学期接口
router.post("/create", function (req, res, next) {
  createSemester(req.body, decoded(req).cardId)
    .then((data) => {
      new Result("操作成功").success(res);
    })
    .catch((err) => {
      console.log(err);
      new Result("操作失败").fail(res);
    });
});

//删除学期接口
router.post("/delete", function (req, res, next) {
  deleteSemester(req.body)
    .then((data) => {
      new Result("操作成功").success(res);
    })
    .catch((err) => {
      new Result("操作失败").fail(res);
    });
});

module.exports = router;
